package com.dao.imp;

import com.dao.FindGoodsDao;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.entity.Good;
import com.util.DBConn;

import java.sql.ResultSet;
import java.util.Map;


/**
 * @author VE111
 */
public class FindGoodsDaoImp implements FindGoodsDao {
    @Override
    public List findGoodsByName(String goodName){
        String sql = "select * from goods where good_name LIKE '%" + goodName + "%'";
//        System.out.println("sql:" + sql);
        return findGoods(sql);
    }
    @Override
    public List findGoodsByParentType(String parentType){
        String sql = "select * from goods where good_parentType='" + parentType + "'";
//        System.out.println("sql:" + sql);
        return findGoods(sql);
    }

    public List findGoodsByParentTypeIsUpload(String parentType){
        String sql = "select * from goods where status=1 and good_parentType='" + parentType + "'";
//        System.out.println("sql:" + sql);
        return findGoods(sql);
    }

    public List findGoodsByChildType(String childType){
        String sql = "select * from goods where good_childType='" + childType + "'";
//        System.out.println("sql:" + sql);
        return findGoods(sql);
    }

    public List findGoodById(int id){
        String sql = "select * from goods where good_id='" + id + "'";
        return findGoods(sql);
    }

    public List<Map> findGoods(String sql) {
        List<Map> listOfRows = new ArrayList();
        try {
            DBConn.init();
            ResultSet rs = DBConn.selectSql(sql);
            listOfRows = extractData(rs);

        } catch (Exception e) {
//            DBConn.closeConn();
            e.printStackTrace();
        }
//        DBConn.closeConn();
        return listOfRows;
    }
    //ResultSet to Array
    public List extractData(ResultSet rs) throws SQLException {
        ResultSetMetaData md = rs.getMetaData();
        int num = md.getColumnCount();
        List listOfRows = new ArrayList();
        while (rs.next()) {
            Map mapOfColValues = new HashMap(num);
            for (int i = 1; i <= num; i++) {
                mapOfColValues.put(md.getColumnName(i), rs.getObject(i));
            }
            listOfRows.add(mapOfColValues);
        }
        return listOfRows;
    }


    public List<Good> getGoodList(String sql){
        List<Good> goodList = new ArrayList<>();
        try{
            DBConn.init();
            ResultSet resultSet = DBConn.selectSql(sql);
//            System.out.println(resultSet.getRow());
            while (resultSet.next()){
                Good good = new Good();
                good.setGood_id(resultSet.getInt("good_id"));
//                System.out.println(good.toString());
                good.setMerchant_id(resultSet.getInt("merchant_id"));
                good.setGood_name(resultSet.getString("good_name"));
                good.setGood_information(resultSet.getString("good_information"));
                good.setGood_price(resultSet.getDouble("good_price"));
                good.setGood_sales(resultSet.getInt("good_sales"));
                good.setGood_remainingAmount(resultSet.getInt("good_remainingAmont"));
                good.setGood_parentType(resultSet.getString("good_parentType"));
                good.setGood_childType(resultSet.getString("good_childType"));
                good.setSubtitle(resultSet.getString("subtitle"));
                good.setGood_url(resultSet.getString("imgUrls"));
                good.setStatus(resultSet.getInt("status"));
                goodList.add(good);
            }
        }catch (Exception e){

        }
        return  goodList;
    }

    public Good getGood(String sql){
        Good good = new Good();
        try{
            DBConn.init();
            ResultSet resultSet = DBConn.selectSql(sql);
            while (resultSet.next()){
                good.setGood_id(resultSet.getInt("good_id"));
                good.setMerchant_id(resultSet.getInt("merchant_id"));
                good.setGood_name(resultSet.getString("good_name"));
                good.setGood_information(resultSet.getString("good_information"));
                good.setGood_price(resultSet.getDouble("good_price"));
                good.setGood_sales(resultSet.getInt("good_sales"));
                good.setGood_remainingAmount(resultSet.getInt("good_remainingAmont"));
                good.setGood_parentType(resultSet.getString("good_parentType"));
                good.setGood_childType(resultSet.getString("good_childType"));
                good.setSubtitle(resultSet.getString("subtitle"));
                good.setGood_url(resultSet.getString("imgUrls"));
                good.setStatus(resultSet.getInt("status"));
            }
        }catch (Exception e){

        }
        return good;
    }

    public static int getGoodsNumber(){
        String sql = "SELECT COUNT(*) FROM goods";
        int count = 0;
        DBConn.init();
        count = DBConn.countSql(sql);
        return count;
    }

}
